<?php
/**
 * Samble HTML
 * Samble html factory class file.
 *
 * @author Lim Afriyadi
 * @package Samble
 * @subpackage js
 * @version pre-alpha
 * @license GNU/GPL.
 */
class JS
{
    private static $o_Instance;
    /**
     * Array of registered javascripts.
     * @var array $a_Script
     */
    var $a_Script;
    /**
     * Text of javascript.
     * @var string $s_Script
     */
    var $s_Script;
    /**
     * Path to include javascript.
     * @var string $s_Path
     */
    var $s_Path;
    /**
     * System pre-registered javascripts.
     * @var array $sSysJs
     */
    var $a_Sys_Js = array();

    private function __CONSTRUCT() { }
    /**
     * To get the single instance of JS class.
     * @return $o_Instance
     * @static
     */
    public static function getInstance()
    {
        if(!self::$o_Instance)
        {
            self::$o_Instance = new JS();
        }
        return self::$o_Instance;
    }
    /**
     * Set the javascript file path to be included.
     * @param string $s_Path
     * @return boolean
     */
    public function setPath($s_Path="")
    {
        if($s_Path != "")
        {
            $this->s_Path = $s_Path;
            return true;
        }
        return false;
    }
    /**
     * Get the javascript preset file path.
     * @return string || false if no path included.
     */
    public function getPath()
    {
        if(isset($this->s_Path))
        {
            return $this->s_Path;
        }
        return false;
    }
    /**
     * Statically include the javascript onto a html page.
     * @param string $s_Path
     * @return boolean
     */
    public function include_js($s_Path)
    {
        if($this->setPath($s_Path))
        {
            echo "\n".'<script type="text/javascript" language="javascript" src="'.$this->getPath().'"></script>'."\n";
            return true;
        }
        return false;
    }
    /**
     * Statically print the javascript onto a html page.
     * @param string $s_Script
     * @return boolean
     */
    public function print_js($s_Script="")
    {
        if($s_Script != "")
        {
            $s_Js  = "<script language=\"javascript\" type=\"text/javascript\">";
            $s_Js .= $s_Script;
            $s_Js .= "</script>\n";
            echo $s_Js;
            return true;
        }
        ErrorHandler::error("No script is defined!");
        return false;
    }
    /**
     * Register a javascript file onto this class javascript array
     * To be included dynamically when a html page loads.
     * @param string $s_Name
     * @param string $s_Path
     * @return boolean
     */
    public function register_js($s_Name="",$s_Path="")
    {
        if($s_Name != "" && $s_Path != "")
        {
            if(file_exists($s_Path))
            {
                $this->a_Script[$s_Name] = $s_Path;
                return true;
            }
            ErrorHandler::error("File ('".$s_Path."') is not exists!");
        }
        else
        {
            ErrorHandler::error("Register javascript failed! Invalid parameters.");
        }
        return false;
    }
    /**
     * Remove a javascript file from this class javascript array.
     * @param string $s_Name
     * @return boolean
     */
    public function unreg_js($s_Name)
    {
        if(array_search($s_Name, $this->a_Script))
        {
            unset($this->a_Script[$s_Name]);
            $this->a_Script = array_values($this->a_Script);
            return true;
        }
        ErrorHandler::error("Script ('".$s_Name."') is not registered!");
        return false;
    }
    /**
     * Include all javascript file from this class javascript registered array
     */
     public function include_all()
     {
         foreach($this->a_Script as $s_Script)
         {
             $s_Html  = '<script';
             $s_Html .= ' src="'.$s_Script.'"';
             $s_Html .= ' language="javascript"/>';
             echo $s_Html;
         }
     }
}
?>